//
//  videoTableViewCell.m
//  XWZ_News_Work
//
//  Created by dllo on 16/1/11.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "videoTableViewCell.h"

@interface videoTableViewCell()

@property (nonatomic, strong) UIImageView *imagePlay;
@property (nonatomic, strong) UIImageView *imageCount;
@property (nonatomic, strong) UIImageView *imageTime;
@property (nonatomic, strong) UIImageView *imageShare;
@property (nonatomic, strong) UILabel *lineLab;
@property (nonatomic, strong) UILabel *borderLab;
@property (nonatomic, strong) UIImageView *playImg;
@property (nonatomic, strong) UIButton *stopBut;
@property (nonatomic, strong) UILabel *playTimeLab;

@property (nonatomic, assign) BOOL JUDGE;

@end

@implementation videoTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubviews];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [self.borderLab mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(@5);
        make.left.equalTo(@5);
        make.right.equalTo(self).offset(-5);
        make.height.equalTo(self).offset(-5);
        
    }];
    
    [self.imgPic mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(@10);
        make.left.equalTo(@10);
        make.right.equalTo(self).offset(-10);
        make.height.equalTo(@250);
        
    }];
    
    
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(@10);
        make.top.equalTo(self.imgPic.mas_bottom).offset(10);
        make.right.equalTo(@10);
        make.height.equalTo(@30);
    }];
    
    [self.desLab mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.titleLab.mas_bottom).offset(0);
        make.left.equalTo(self.titleLab.mas_left);
        make.right.equalTo(self.mas_right).offset(-10);
        make.height.equalTo(self.titleLab.mas_height);
        
    }];
    
    [self.lineLab mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.desLab.mas_bottom);
        make.left.equalTo(self.desLab.mas_left);
        make.right.equalTo(self.mas_right).offset(-10);
        make.height.equalTo(@1);

        
    }];
    
    [self.imageTime mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.lineLab.mas_bottom).offset(5);
        make.left.equalTo(self.lineLab.mas_left);
        make.width.equalTo(self.desLab.mas_height);
        make.height.equalTo(self.desLab.mas_height);
        
    }];

    [self.timeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.lineLab.mas_bottom).offset(5);
        make.left.equalTo(self.imageTime.mas_right).offset(5);
        make.width.equalTo(self).multipliedBy(0.15);
        make.height.equalTo(self.desLab.mas_height);
        
    }];
    
    [self.imagePlay mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.lineLab.mas_bottom).offset(5);
        make.left.equalTo(self.timeLab.mas_right).offset(10);
        make.width.equalTo(self.desLab.mas_height);
        make.height.equalTo(self.desLab.mas_height);
        
    }];
    
    [self.playcountLab mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.lineLab.mas_bottom).offset(5);
        make.left.equalTo(self.imagePlay.mas_right).offset(5);
        make.width.equalTo(self).multipliedBy(0.15);
        make.height.equalTo(self.desLab.mas_height);
        
        
    }];
    
    [self.imageCount mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.lineLab.mas_bottom).offset(10);
        make.left.equalTo(self.playcountLab.mas_right).offset(40);
        make.width.equalTo(self.desLab.mas_height).offset(-5);
        make.height.equalTo(self.desLab.mas_height).offset(-5);
        
    }];
    
    [self.wiriteLab mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.lineLab.mas_bottom).offset(5);
        make.left.equalTo(self.imageCount.mas_right).offset(5);
        make.width.equalTo(self).multipliedBy(0.15);
        make.height.equalTo(self.desLab.mas_height);
        
    }];
    
    [self.imageShare mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.lineLab.mas_bottom).offset(10);
        make.right.equalTo(self.mas_right).offset(-10);
        make.width.equalTo(self.desLab.mas_height).offset(-10);
        make.height.equalTo(self.desLab.mas_height).offset(-5);
        
    }];
    
    self.playImg.frame = CGRectMake(self.contentView.frame.size.width / 2 - 20, self.contentView.frame.size.height / 2 - 70, 50, 50);
    
}

- (void)createSubviews
{
    self.borderLab = [UILabel new];
    [self.borderLab.layer setBorderWidth:2.0];
    self.borderLab.layer.borderColor = [UIColor colorWithRed:113 / 255.0 green:120 / 255.0 blue:115 / 255.0 alpha:1.0].CGColor;
    self.borderLab.layer.cornerRadius = 10;
    self.borderLab.layer.masksToBounds = YES;
    [self.contentView addSubview:self.borderLab];
    
    self.imgPic = [UIImageView new];
    [self.contentView addSubview:self.imgPic];
    
    self.titleLab = [UILabel new];
    self.titleLab.font = [UIFont systemFontOfSize:18];
    self.titleLab.textColor = [UIColor blackColor];
    [self.contentView addSubview:self.titleLab];
    
    self.desLab = [UILabel new];
    self.desLab.font = [UIFont systemFontOfSize:16];
    self.desLab.textColor = [UIColor grayColor];
    [self.contentView addSubview:self.desLab];
    
    self.lineLab = [UILabel new];
    self.lineLab.backgroundColor = [UIColor grayColor];
    [self.contentView addSubview:self.lineLab];
    
    self.imagePlay = [UIImageView new];
    self.imagePlay.image = [UIImage imageNamed:@"ibofang"];
    [self.contentView addSubview:self.imagePlay];
    
    self.playcountLab = [UILabel new];
    self.playcountLab.textColor = [UIColor grayColor];
    [self.contentView addSubview:self.playcountLab];
    
    self.imageTime = [UIImageView new];
    self.imageTime.image = [UIImage imageNamed:@"shijian"];
    [self.contentView addSubview:self.imageTime];
    
    self.timeLab  = [UILabel new];
    self.timeLab.textColor = [UIColor grayColor];
    [self.contentView addSubview:self.timeLab];
    
    self.imageCount = [UIImageView new];
    self.imageCount.image = [UIImage imageNamed:@"bianji01"];
    [self.contentView addSubview:self.imageCount];
    
    self.wiriteLab = [UILabel new];
    self.wiriteLab.textColor = [UIColor grayColor];
    [self.contentView addSubview:self.wiriteLab];
    
    self.imageShare = [UIImageView new];
    self.imageShare.image = [UIImage imageNamed:@"fenxiang"];
    [self.contentView addSubview:self.imageShare];
    
    self.playImg = [[UIImageView alloc] init];
    self.playImg.image = [UIImage imageNamed:@"playBut"];
    self.playImg.userInteractionEnabled = YES;
    [self.contentView addSubview:self.playImg];
}

- (void)setVideoM:(videoModel *)videoM
{
    avTools *myTool = [avTools sharePlayer];
    self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:myTool];
    self.playerLayer.frame = self.imgPic.frame;
    self.playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;//视频填充模式
    [self.contentView.layer addSublayer:self.playerLayer];

    
#pragma 播放view
    self.playView = [[UIView alloc] initWithFrame:CGRectMake(self.imgPic.frame.origin.x, self.playerLayer.frame.size.height - 40, self.imgPic.frame.size.width, 40)];
    self.playView.backgroundColor = [UIColor blackColor];
    self.playView.alpha = 0.6;
//    [self.contentView addSubview:self.playView];
    
#pragma 播放暂停 按钮
    self.stopBut = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.stopBut.frame = CGRectMake(5, 5, 30, 30);
    [self.stopBut setImage:[UIImage imageNamed:@"zantingbofang"] forState:(UIControlStateNormal)];
    [self.stopBut setImage:[UIImage imageNamed:@"bofang0"] forState:(UIControlStateSelected)];
    [self.stopBut addTarget:self action:@selector(butAction) forControlEvents:(UIControlEventTouchUpInside)];
    
    self.JUDGE = NO;
//    [self.playView addSubview:self.stopBut];
    [myTool playWithURL:videoM.mp4_url];
}

- (void)butAction
{
    if (self.JUDGE == NO) {
        self.stopBut.selected = YES;
        [[avTools sharePlayer] pause];
        self.JUDGE = YES;
    } else {
        [[avTools sharePlayer] play];
        self.JUDGE = NO;
        self.stopBut.selected = NO;
    }
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
